/*-------------------------------------------------------------------------
 *
 * crypt.h
 *	  接口到 libpq/crypt.c
 *
 * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
 * Portions Copyright (c) 1994, Regents of the University of California
 *
 * src/include/libpq/crypt.h
 *
 *-------------------------------------------------------------------------
 */
#ifndef PG_CRYPT_H
#define PG_CRYPT_H

#include "datatype/timestamp.h"


/*
 * 密码哈希或秘密的类型。
 *
 * 明文密码可以通过用户在 CREATE/ALTER USER
 * 命令中传递。它们将在存储到磁盘之前被加密为 MD5 或 SCRAM-SHA-256 格式，因此只有 MD5 和 SCRAM-SHA-256 密码应该出现在 pg_authid.rolpassword 中。它们也是 password_encryption GUC 的允许值。
 */
typedef enum PasswordType
{
	PASSWORD_TYPE_PLAINTEXT = 0,
	PASSWORD_TYPE_MD5,
#ifdef FDDGM
	PASSWORD_TYPE_SM3,
#endif
	PASSWORD_TYPE_SCRAM_SHA_256
} PasswordType;

extern PasswordType get_password_type(const char *shadow_pass);
extern char *encrypt_password(PasswordType target_type, const char *role,
							  const char *password);

extern char *get_role_password(const char *role, const char **logdetail);

extern int	md5_crypt_verify(const char *role, const char *shadow_pass,
							 const char *client_pass, const char *md5_salt,
							 int md5_salt_len, const char **logdetail);
extern int	plain_crypt_verify(const char *role, const char *shadow_pass,
							   const char *client_pass,
							   const char **logdetail);

#ifdef FDDGM
extern int	sm3_crypt_verify(const char *role, const char *shadow_pass,
							 const char *client_pass, const char *salt,
							 int salt_len,const char **logdetail);
#endif

#endif
